<?php
namespace app\index\controller;


use app\index\model\User;
use app\index\model\CustomerMapping;
use app\index\model\Customer;
class Test
{
    public function index()
    {
        echo phpinfo();
    }


    public function getO()
    {
        $data = array('title'=>'修改的第一条记录', 'by'=>'xiaozhang');
        $mod = new CustomerMapping();
        $msg = $mod->getOne($data);

        var_dump($msg);
    }

    public function getM()
    {
        $data = array( 'title'=>'我的tp5的第三个记录');

        $mod = new CustomerMapping();
        $msg = $mod->getAll($data);

        var_dump($msg);
    }

    public function modify()
    {
        $conditions = array('title'=>'修改的第一条记录', 'by'=>'xiaozhang');
        $data = array('title'=>'修改的第二条记录');

        $mod = new CustomerMapping();
        $msg = $mod->modifyData($conditions, $data);

        var_dump($msg);

    }

    public function ins()
    {

        $mod = new  CustomerMapping();

        $i = 0;
        for ($i;$i<200;$i++){
            $customerId = $mod->genAutoIncrementId();

            var_dump($customerId);

            $data = array(
                'title'=> "我的tp5的第三个记录",
                'by'   => "xiaozhang",
                'id'   => $customerId,
            );

            $res = $mod->insertData($data);

            var_dump($res.'---------'.$i);
        }

    }



    public function getId()
    {

//        $this->getNextId();

        $mod = new CustomerMapping();
        $msg = $mod->getNewId();
        var_dump($msg);
    }


    function getNextId($mongo,$name,$param=array()){

        $param += array(   //默认ID从1开始,间隔是1
            'init' => 1,
            'step' => 1,
        );

        $update = array('$inc'=>array('id'=>$param['step']));   //设置间隔
        $query = array('name'=>$name);
        $command = array(
            'findandmodify' => 'ids',
            'update' => $update,
            'query' => $query,
            'new' => true
        );

        $id = $mongo->db->command($command);
        if (isset($id['value']['id'])) {
            return $id['value']['id'];
        }else{
            $mongo->insert(array(
                'name' => $name,
                'id' => $param['init'],     //设置ID起始数值
            ));
            return $param['init'];
        }
    }
}

